﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.Auth
{
    public partial class SSRP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {
            if (RequiredFieldValidator1.IsValid == false || RequiredFieldValidator2.IsValid == false ||
                RequiredFieldValidator3.IsValid == false || RequiredFieldValidator4.IsValid == false ||
                    RequiredFieldValidator5.IsValid == false || RegularExpressionValidator1.IsValid == false ||
                    CompareValidator1.IsValid == false) return;
        
            UserBLL BLL = new UserBLL();
            try
            {
                Guid UserNum=BLL.userGetByLogin(UserNameTextBox.Text)[0].user_num;
                string UserID = BLL.userGetByNum(UserNum)[0].user_id;
                string Email = BLL.userGetByNum(UserNum)[0].user_email;
                if (UserID != IDTextBox.Text || Email != EmailTextBox.Text)
                {
                    StatusLabel.Text ="Error: Wrong Details or User not exist";
                    StatusLabel.Visible = true;
                    return;
                }
                else
                {
                    string Body = "<br /><span style=\"color: #000066\">Hi,";
                    Body+="<br /><br />You have requested to Recover your Login Password to the WareHouse.<br /><br /></span>";
                    Body += "<span style=\"font-weight: bold; color: #ff0000\"> Your new Password is: ";
                    Body += PasswordTextBox.Text + ".</span><br />";
                    BLL.UserUpdatePassword(UserNum, PasswordTextBox.Text);
                    Boolean res = Mail.SendMail(Email, "WareHouse Password Recovery",Body);
                    if (res == false)
                    {
                        StatusLabel.Text = "Your new password was Updated but failed to be sent to your E-mail";
                    }
                    else
                    {
                        StatusLabel.Text = "Your new password was Updated and sent to your E-mail";
                    }
                    StatusLabel.Visible = true;
                }

            }
            catch(Exception)
            {
                StatusLabel.Text = "Error: Wrong Details or User not exist";
                StatusLabel.Visible = true;
            }
        }
    }
}
